﻿
using Dapper;
using HR.IServices.PengIService;
using HR.Models.PengModels;
using System.Data.SqlClient;

namespace HR.Services.PengService
{
    public class ZhiWeiFenLeiServices : IZhiWeiFenLeiServices
    {
        private readonly IConfiguration conf;
        private readonly string? conStr;

        public ZhiWeiFenLeiServices(IConfiguration conf)
        {
            this.conf = conf;
            conStr = this.conf["ConnectionStrings"];
        }
        public Task<int> Add(ZhiWeiFenLei zhiWeiFenLei)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($" insert into config_major_kind(major_kind_id, major_kind_name) values('{zhiWeiFenLei.Major_kind_id}','{zhiWeiFenLei.Major_kind_name}')");
                }
            });
        }

        public Task<int> Delete(int mfk_id)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"delete from config_major_kind where mfk_id='{mfk_id}'");
                }
            });
        }

        public Task<List<ZhiWeiFenLei>> FindAll()
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr)) 
                {
                    return con.Query<ZhiWeiFenLei>($"select * from config_major_kind").ToList();
                }
            });
        }

        public Task<int> Update(ZhiWeiFenLei zhiWeiFenLei)
        {
            return Task.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    return con.Execute($"update config_major_kind set major_kind_id ='{zhiWeiFenLei.Major_kind_id}', major_kind_name = '{zhiWeiFenLei.Major_kind_name}' where mfk_id='{zhiWeiFenLei.Mfk_id}'");
                }
            });
        }
    }
}
